import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '../views/Login.vue'

const routes = [
  {
    path:'/',
    redirect:'/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/index',
    name: 'Index',
    component: () => import('../views/Index.vue'),
    children:[
      {
        path: '/index',
        redirect:'/welecome'
      },
      {
        path:'/welecome',
        name: 'Welecome',
        component: () => import('../views/Welecome.vue'),
      },
      {
        path:'/users',
        name: 'Users',
        component: () => import('../views/Page/Users.vue'),
      },
      {
        path:'/roles',
        name: 'Roles',
        component: () => import('../views/Page/Roles.vue'),
      },
      {
        path:'/rights',
        name: 'Rights',
        component: () => import('../views/Page/Rights.vue'),
      },
      {
        path:'/goods',
        name: 'Goods',
        component: () => import('../views/Page/Goods.vue'),
      },
      {
        path:'/params',
        name: 'Params',
        component: () => import('../views/Page/Params.vue'),
      },
      {
        path:'/categories',
        name: 'Categories',
        component: () => import('../views/Page/Categories.vue'),
      },
      {
        path:'/add',
        name: 'Add',
        component: () => import('../views/Page/Add.vue'),
      },
      {
        path:'/reports',
        name: 'Reports',
        component: () => import('../views/Page/Reports.vue'),
      }
    ]
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})


const whiteList = ['/login'];
router.beforeEach((to,form,next)=>{
  let users = JSON.parse(localStorage.getItem('users'));
  if(users){
    next();
  }else{
    if(whiteList.indexOf(to.path) != -1){
      next();
    }else{
      next('/login');
    }
  }
})

export default router
